package com.system.demo.mapper.auth;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.system.demo.entity.auth.MenuList;
import com.system.demo.entity.auth.MenuTreeItem;
import com.system.demo.entity.auth.SystemMenu;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface SystemMenuMapper extends BaseMapper<SystemMenu> {
    @Select("SELECT " +
            "sm.id, " +
            "sm.label, " +
            "sm.path, " +
            "sm.value " +
            "FROM " +
            "(SELECT DISTINCT menu_father_id FROM system_menu_connection) unique_father_ids " +
            "JOIN " +
            "system_menu sm ON unique_father_ids.menu_father_id = sm.id " +
            "LEFT JOIN " +
            "system_menu sf ON unique_father_ids.menu_father_id = sf.id " +
            "ORDER BY " +
            "unique_father_ids.menu_father_id, sm.id")
    List<MenuList> getMenuListWithTopMenu();

    @Select("SELECT " +
            "sm.id AS id, " +
            "sm.label, " +
            "sm.path, " +
            "sm.value " +
            "FROM " +
            "system_menu_connection sc " +
            "JOIN " +
            "system_menu sm ON sc.menu_son_id = sm.id " +
            "WHERE " +
            "sc.menu_father_id = #{parentId} " +
            "ORDER BY " +
            "sm.id")
    List<SystemMenu> getMenuListByParentId(@Param("parentId") Integer parentId);


    @Select({
            "SELECT DISTINCT sm.value",
            "FROM user_role ur",
            "JOIN system_role sr ON ur.role_id = sr.role_id AND ur.is_available = 1 AND sr.is_available = 1",
            "JOIN system_role_menu srm ON sr.role_id = srm.role_id",
            "JOIN system_menu sm ON srm.menu_id = sm.id",
            "WHERE ur.uid = #{uid}"
    })
    List<String> getMenuValuesByUserId(@Param("uid") String uid);

    @Select("SELECT sm.id, sm.path, sm.label, sm.value, smc.menu_father_id " +
            "FROM system_menu sm " +
            "LEFT JOIN system_menu_connection smc " +
            "ON sm.id = smc.menu_son_id")
    List<Map<String, Object>> getAllMenuConnections();

}
